package factory

import (
	"log/slog"
	"quipus/internal/llm"
	"quipus/internal/llm/provider/baidu"
	"quipus/internal/llm/provider/kimi"
	"quipus/internal/llm/provider/ollama"
	"quipus/internal/llm/provider/openai"
	"quipus/internal/llm/provider/zhipu"
)

type EmbeddingClientConfig struct {
	Url      string
	AuthInfo string
	Model    string
	API      string
	Dim      int
}

type ChatClientConfig struct {
	Url       string
	AuthInfo  string
	Model     string
	API       string
	MaxTokens int
}

func NewEmbeddingClient(config *EmbeddingClientConfig) llm.LLM_Embedding {
	var client llm.LLM_Embedding
	var err error
	embedConfig := &llm.LLMClientConfig{
		AuthInfo: config.AuthInfo,
		BaseUrl:  config.Url,
		Model:    config.Model,
	}
	switch config.API {
	case "zhipu":
		client, err = zhipu.NewClient(embedConfig)
		// user, err := s.api.Gds.Handler.GetUser(s.ctx, &apb.FindUser{Username: "zhipu"})
		if err != nil {
			slog.Error("Failed to get user", err)
			return nil
		}
	case "ollama":
		client, err = ollama.NewClient(embedConfig)
		if err != nil {
			slog.Error("Failed to create kimi client", err)
			return nil
		}

	default:
		slog.Error("Unsupported assistant provider")
		return nil

	}
	return client
}

func NewChatClient(config *ChatClientConfig) llm.LLM_Chat {
	var client llm.LLM_Chat
	var err error
	queryConfig := &llm.LLMClientConfig{
		AuthInfo: config.AuthInfo,
		BaseUrl:  config.Url,
		Model:    config.Model,
	}

	switch config.API {
	case "zhipu":
		client, err = zhipu.NewClient(queryConfig)
		// user, err := s.api.Gds.Handler.GetUser(s.ctx, &apb.FindUser{Username: "zhipu"})
		if err != nil {
			slog.Error("Failed to get user", err)
			return nil
		}
	case "ollama":
		client, err = ollama.NewClient(queryConfig)
		if err != nil {
			slog.Error("Failed to create kimi client", err)
			return nil
		}
	case "baidu":
		client, err = baidu.NewClient(queryConfig)
		if err != nil {
			slog.Error("Failed to create kimi client", err)
			return nil
		}
	case "kimi":
		client, err = kimi.NewClient(queryConfig)
		if err != nil {
			slog.Error("Failed to create kimi client", err)
			return client
		}
	case "openai":
		client, err = openai.NewClient(queryConfig)
		if err != nil {
			slog.Error("Failed to create kimi client", err)
			return client
		}
	default:
		slog.Error("Unsupported assistant provider")
		return nil

	}
	return client
}
